package com.approval.approvalapp.center.acitivty;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.center.adapter.ProvinceCensusAdapter;
import com.approval.approvalapp.center.bean.CenterIndexBean;
import com.approval.approvalapp.center.bean.ProvinceCensusBean;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ProvinceCensusActivity extends BaseActivity implements AppBarLayout.OnOffsetChangedListener {


    public static void goInto(Context context){
        Intent intent = new Intent();
        intent.setClass(context,ProvinceCensusActivity.class);
        context.startActivity(intent);
    }

    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.tv_head1)
    TextView tvHead1;
    @Bind(R.id.tv_head2)
    TextView tvHead2;
    @Bind(R.id.tv_head3)
    TextView tvHead3;
    @Bind(R.id.tv_head4)
    TextView tvHead4;
    @Bind(R.id.collapsing_toolbar)
    CollapsingToolbarLayout collapsingToolbar;
    @Bind(R.id.appbar)
    AppBarLayout appbar;
    @Bind(R.id.recyclerview)
    XRecyclerView mRecyclerView;
    @Bind(R.id.main_content)
    CoordinatorLayout mainContent;
    @Bind(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    private LinearLayoutManager mLayoutManager;

    List<ProvinceCensusBean.TwoBean> mList = new ArrayList<>();
    private ProvinceCensusAdapter mAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.province_census_layout;
    }

    @Override
    public void initView() {
        initXR();
        backIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void initData() {
        getData();
    }

    public void getData(){
        JSONObject jsonObject = new JSONObject();
        NetRequsetUtil.getInstance().netRequestPostJson("v1/center/hall_list", jsonObject.toString(), new NetDownResponse() {
            public String TAG = getClass().getSimpleName();

            @Override
            public void success(String str) {
                Log.d(TAG, "success: "+ str );

                Gson gson = new Gson();
                ProvinceCensusBean provinceCensusBean = gson.fromJson(str, ProvinceCensusBean.class);
                List<ProvinceCensusBean.OneBean> one = provinceCensusBean.getOne();
                List<ProvinceCensusBean.TwoBean> two = provinceCensusBean.getTwo();
                mList.clear();
                mList.addAll(two);
                mAdapter.notifyDataSetChanged();

                if(one != null && one.size() > 0){
                    ProvinceCensusBean.OneBean oneBean = one.get(0);

                    tvHead1.setText(oneBean.getDTMJ() +"");
                    tvHead2.setText(oneBean.getJZBM() +"");
                    tvHead3.setText(oneBean.getCKRS() +"");
                    tvHead4.setText(oneBean.getZXRS() +"");

                }

            }

            @Override
            public void errowithresponse(String str) {

            }

            @Override
            public void erro() {

            }

            @Override
            public void finish() {
                if (swipeRefreshLayout != null){
                    swipeRefreshLayout.setRefreshing(false);
                    mRecyclerView.loadMoreComplete();
                }
            }
        });

    }

    private void initXR() {
        mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(mLayoutManager);

        mRecyclerView.setPullRefreshEnabled(false);
        mRecyclerView.setLoadingMoreEnabled(false);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mRecyclerView.setNoMore(false);
                        getData();
                    }
                }, 500);
            }
        });
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {

            }
        });

        appbar.addOnOffsetChangedListener(this);

        mAdapter = new ProvinceCensusAdapter(mList,this);
        mRecyclerView.setAdapter(mAdapter);
    }


    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if ( verticalOffset == 0 ) {
            swipeRefreshLayout.setEnabled(true);
        } else  {
            swipeRefreshLayout.setEnabled(false);
        }

    }
}
